package com.hnkywl.sts.service.sys;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.organ.BmfwDao;
import com.hnkywl.sts.entity.organ.Bmfw;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-20
 * Time: 下午7:40
 * To change this template use File | Settings | File Templates.
 */
@Service
public class BmfwService {
    private BmfwDao bmfwDao;

    public String getWzbmTreeJson(Long gsId, Long wzbmId, int type) {
        String bm = type == 1?"yjwzbm":(type == 2?"rjwzbm":"pm");
        String sjbm = type == 2?"yjwzbm_id":(type == 3?"rjwzbm_id":"");
        List<Map> tree = new ArrayList<Map>();
        if(type != 1 && !bmfwDao.existChildren(gsId,type,wzbmId,bm,sjbm)){
            tree.addAll(bmfwDao.findTreejson(null,null,wzbmId,bm,sjbm));
        } else {
            tree.addAll(bmfwDao.findTreejson(gsId,type,wzbmId,bm,sjbm));
        }
        List<Map> treeJson = new ArrayList<Map>();
        for(Map m : tree){
            Map item = new HashMap();
            item.put("id",m.get("wzbmId")+":"+type+":"+gsId);
            item.put("text",m.get("name")+"("+m.get("code")+")"+(m.get("gysmc")!=null?("("+m.get("gysmc")+")"):""));
            item.put("state",type==3?"open":"closed");
            Map attributes = new HashMap();
            attributes.put("level",""+type);
            attributes.put("children","true");
            item.put("attributes",attributes);
            treeJson.add(item);
        }
        return new Gson().toJson(treeJson);
    }

    public String getWzbmTreeJsonByYg(Long ygId, Long wzbmId, int type) {
        String bm = type == 1?"Y":(type == 2?"R":"P");
        String sjbm = type == 2?"Y.ID":(type == 3?"R.ID":"");
        List<Map> tree = new ArrayList<Map>();
        tree.addAll(bmfwDao.findTreejsonByYg(ygId,type,wzbmId,bm,sjbm));
        List<Map> treeJson = new ArrayList<Map>();
        for(Map m : tree){
            Map item = new HashMap();
            item.put("id",m.get("ID")+":"+type);
            item.put("text","("+m.get("CODE")+")"+m.get("NAME"));
            item.put("state",type==3?"open":"closed");
            Map attributes = new HashMap();
            attributes.put("level",""+type);
            attributes.put("children","true");
            item.put("attributes",attributes);
            treeJson.add(item);
        }
        return new Gson().toJson(treeJson);
    }

    @Transactional
    public void save(List<Bmfw> bmfwList){
        if(bmfwList != null && bmfwList.size() != 0){
            bmfwDao.deleteByGsId(bmfwList.get(0).getGsId());
        }
        if(bmfwList != null && bmfwList.size() != 0&&bmfwList.get(0).getWzbmId() != 0){
            for(Bmfw bmfw:bmfwList){
                bmfwDao.save(bmfw);
            }
        }
    }

    @Autowired
    public void setBmfwDao(BmfwDao bmfwDao) {
        this.bmfwDao = bmfwDao;
    }
}
